package com.syezon.wifikey.speed;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.RelativeLayout;

import com.syezon.wifikey.R;
import com.syezon.wifikey.analysis.AnalysisHelper;
import com.syezon.wifikey.analysis.event.EventEnum;
import com.syezon.wifikey.base.BaseActivity;
import com.syezon.wifikey.utils.StatusBarUtil;

import butterknife.BindView;
import butterknife.OnClick;

public class SpeedTestActivity extends BaseActivity implements SpeedTestFragment.SpeedTestFinishListener {

    public static final String ARG_SSID = "arg_ssid";

    @BindView(R.id.rl_top)
    RelativeLayout mRlTop;
//    private SpeedTestFragment mSpeedTestFragment;
    private NewSpeedtestFragment mNewSpeedTestFragment;
    private String mSsid;


    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_speed_test;
    }

    @Override
    protected void initViewsAndEvents() {
        Bundle extras = getIntent().getExtras();
        mSsid = extras.getString(ARG_SSID);
        if (TextUtils.isEmpty(mSsid)) {
            finish();
            return;
        }
        StatusBarUtil.setPaddingSmart(this, mRlTop);
//        mSpeedTestFragment = SpeedTestFragment.newInstance(mSsid);
//        mSpeedTestFragment.setFinishListener(this);
        mNewSpeedTestFragment = NewSpeedtestFragment.newInstance();
        getSupportFragmentManager().beginTransaction().add(R.id.fl_container, mNewSpeedTestFragment).commit();
        AnalysisHelper.onEvent(EventEnum.SPEED_TEST_SHOW.eventId);
    }

    @OnClick({R.id.iv_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
        }
    }

    @Override
    public void finish(final TestResultInfo resultInfo) {
        getHandler().postDelayed(new Runnable() {
            @Override
            public void run() {
                getSupportFragmentManager().beginTransaction().setCustomAnimations(R.anim.anim_top_in, R.anim.anim_bottom_out).replace(R.id.fl_container, SpeedResultFragment.newInstance(resultInfo))
                        .commitAllowingStateLoss();
            }
        }, 1500);

    }

    @Override
    public void cancel() {
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        getHandler().removeCallbacksAndMessages(null);
    }
}
